﻿using Sys.Common;
using Sys.Core.Dri;
using Sys.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Sys.Api.Dri
{
    public class CarApi : BaseApi
    {
        public string Grid(int rows, int page)
        {
            var entities = CarBll.I.SortEntities;

            var list = CarBll.I.EntitiesPage(entities, rows, page);
            foreach (DriCar item in list)
            {
                item.TeacherName = TeacherBll.I.GetNameByID(item.TeacherID);
            }
            return GetGrid(entities.Count(), list);
        }

        public string ComboBoxList()
        {
            var entities = CarBll.I.SortEntities;
            List<JsonMap> jms = new List<JsonMap>();
            jms.Add(new JsonMap("ID", "id"));
            jms.Add(new JsonMap("CarNum", "text"));
            return CustomJson(entities.ToList(), jms, false);
        }

        public string Save(DriCar model)
        {
            JsonMsg result = new JsonMsg();

            if (string.IsNullOrEmpty(model.ID))
            {
                result.Message = "添加成功!";

                model.ID = StringHelper.GetGuid();

                //model.IntSort = ButtonBll.I.MaxSort(m => m.IntSort);
                model.InsertTime = DateTime.Now;

                result.Success = CarBll.I.Insert(model) > 0;
            }
            else
            {
                result.Message = "修改成功!";

                result.Success = CarBll.I.Update(model) > 0;
            }

            if (!result.Success) { result.Message = "操作失败!"; }

            return JsonHelper.ToJson(result);
        }

        public string Delete(string id)
        {
            JsonMsg result = new JsonMsg();

            result.Success = CarBll.I.Delete(id) > 0;

            if (result.Success) { result.Message = "删除成功!"; }

            return JsonHelper.ToJson(result);
        }

        public string EnableOrDisable(string id, bool bit)
        {
            JsonMsg result = new JsonMsg();

            DriCar model = CarBll.I.GetByKey(id);

            if (model != null)
            {
                if (model.IsVisible != bit)
                {
                    model.IsVisible = bit;

                    result.Success = CarBll.I.Update(model) > 0;

                    if (result.Success)
                    {
                        result.Message = bit ? "启用成功!" : "禁用成功!";
                    }
                }
                else
                {
                    result.Message = bit ? "已经是启用状态!" : "已经是禁用状态!";
                }
            }

            return JsonHelper.ToJson(result);
        }

        /// <summary>
        /// 导出
        /// </summary>
        public void Export()
        {
            var entities = CarBll.I.SortEntities;
            foreach (DriCar item in entities)
            {
                item.TeacherName = TeacherBll.I.GetNameByID(item.TeacherID);
            }
            var columns = new List<string[]>();
            columns.Add(new string[] { "CarNum", "车牌号" });
            columns.Add(new string[] { "CarType", "车型" });
            columns.Add(new string[] { "BrandModel", "品牌型号" });
            columns.Add(new string[] { "CarFrameNum", "车架号" });
            columns.Add(new string[] { "MotorNum", "发动机号" });
            columns.Add(new string[] { "RegisterDate", "注册时间" });
            columns.Add(new string[] { "CertificateDate", "发证日期" });
            columns.Add(new string[] { "AbandonDate", "报废日期" });
            columns.Add(new string[] { "TeacherName", "对应教练员" });
            columns.Add(new string[] { "Description", "描述" });
            columns.Add(new string[] { "IsVisible", "启用" });
            columns.Add(new string[] { "InsertTime", "创建时间" });
            NopiHelper.Export(columns, entities.ToList());
        }
    }
}
